package hyj.qx.websocket.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import hyj.qx.websocket.enums.ErrorCode;
import hyj.qx.websocket.entity.User;
import hyj.qx.websocket.mapper.UserMapper;
import hyj.qx.websocket.service.UserService;
import hyj.qx.websocket.vo.JsonResult;
import hyj.qx.websocket.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping(value ="/login")
    @ApiOperation("登录用户")
    public JsonResult<User> login(@RequestBody User user, HttpSession session){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",user.getUserName()).eq("password",user.getPassword());
        User user1=userMapper.selectOne(queryWrapper);
      if (!StrUtil.isBlankIfStr(user1)){
          session.setAttribute("user",user.getUserName());
          return JsonResult.success(user1);
      }
      return JsonResult.Failed(null,ErrorCode.USER_NOT_FOUND);
    }
    @RequestMapping("/getUsername")
    public String getUsername(HttpSession session){
        String username = (String) session.getAttribute("user");
        return username;
    }
    @PostMapping("add")
    @ApiOperation("添加用户")
    public JsonResult<Integer> add(@RequestBody User user){
        Integer add = userService.add(user);
        if (add!=null) {
            return JsonResult.success(add);
        }
        return JsonResult.Failed(null,ErrorCode.USER_ADDNOT);
    }
    @PostMapping("getuserid")
    @ApiOperation("得到用户id")
    public JsonResult<Integer> getUserid(@RequestParam String userName){
       Integer ID= userMapper.getIdByName(userName);
       return JsonResult.success(ID);
    }


    // 修改接口
    @PostMapping("edit")
    @ApiOperation("修改用户")
    public JsonResult<User> edit(@RequestBody User user){
        User edit = userService.updetByUserId(user);
        if(edit!=null){
            return JsonResult.success(edit);
        }
        return JsonResult.Failed(null,ErrorCode.USER_UPDATENOT);
    }

    // 删除接口
    @GetMapping("delete")
    @ApiOperation("删除用户")
    public JsonResult<Integer> delete(String userId){
        int deleteOne = userService.deleteOne(userId);
        return JsonResult.success(deleteOne);
    }

    // 详情接口
    @GetMapping("detail")
    @ApiOperation("用户详情")
    public JsonResult<User> detail(String userId){
        User user = userService.selectUser(userId);
        return JsonResult.success(user);
    }

    // 分页查询
    @GetMapping("page")
    @ApiOperation("分页查询")
    public JsonResult<PageVo<User>> page(String userName, Integer pageNumber, Integer pageSize){
        PageVo<User> page = userService.page(userName, pageNumber, pageSize);
        return JsonResult.success(page);
    }

}
